package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogUserBo;
import com.ruoyix.weblog.domain.vo.BlogUserVo;
import com.ruoyix.weblog.domain.BlogUser;
import com.ruoyix.weblog.mapper.BlogUserMapper;
import com.ruoyix.weblog.service.IBlogUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogUserServiceImpl implements IBlogUserService {

    private final BlogUserMapper baseMapper;

    /**
     * 查询用户信息
     */
    @Override
    public BlogUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<BlogUserVo> queryPageList(BlogUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogUser> lqw = buildQueryWrapper(bo);
        Page<BlogUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<BlogUserVo> queryList(BlogUserBo bo) {
        LambdaQueryWrapper<BlogUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogUser> buildQueryWrapper(BlogUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), BlogUser::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), BlogUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNumber()), BlogUser::getPhoneNumber, bo.getPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), BlogUser::getEmail, bo.getEmail());
        lqw.eq(bo.getUserStatus() != null, BlogUser::getUserStatus, bo.getUserStatus());
        lqw.eq(bo.getGender() != null, BlogUser::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), BlogUser::getOpenId, bo.getOpenId());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), BlogUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmire()), BlogUser::getAdmire, bo.getAdmire());
        lqw.eq(StringUtils.isNotBlank(bo.getSubscribe()), BlogUser::getSubscribe, bo.getSubscribe());
        lqw.eq(StringUtils.isNotBlank(bo.getIntroduction()), BlogUser::getIntroduction, bo.getIntroduction());
        lqw.eq(bo.getUserType() != null, BlogUser::getUserType, bo.getUserType());
        lqw.eq(bo.getDeleted() != null, BlogUser::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(BlogUserBo bo) {
        BlogUser add = BeanUtil.toBean(bo, BlogUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(BlogUserBo bo) {
        BlogUser update = BeanUtil.toBean(bo, BlogUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
